package org.bdware.doip.core.model.digitalObject;

import com.google.gson.JsonObject;

public class DOWithMetadata extends DigitalObject{

    public static final String METADATA = "metadata";

    public static DOWithMetadata fromDO(DigitalObject digitalObject){
        if(digitalObject.attributes == null || digitalObject.attributes.get(METADATA) == null){
            return null;
        }
        return (DOWithMetadata)digitalObject;
    }

    public DOWithMetadata(String id, DoType type) {
        super(id, type);
    }

    public void addMetadata(String key, String value){
        if(attributes==null || attributes.get(METADATA) == null){
            addAttribute(METADATA, new JsonObject());
        }
        attributes.get(METADATA).getAsJsonObject().addProperty(key,value);
    }

    public JsonObject getMetadata(){
        if(attributes.get(METADATA) == null) return null;
        return attributes.get(METADATA).getAsJsonObject();
    }

    public String getMetadata(String key){
        if(attributes.get(METADATA) == null) return null;
        return attributes.get(METADATA).getAsJsonObject().get(key).getAsString();
    }

    public void setMetadata(JsonObject jo){
        this.addAttribute(METADATA,jo);
    }

}
